//
//  XCRetweetView.swift
//  XCMicroBo
//
//  Created by 1 on 16/3/12.
//  Copyright © 2016年 1. All rights reserved.
//

import UIKit
import SnapKit

class XCRetweetView: UIView {
    
    //定义一个约束对象
    var bottomConstrain : Constraint?

    
    //定义一个模型属性用来给转发微博赋值
    var model : status? {
        
        didSet{
            
            retweetLabel.text = model?.text
            
            retweetPicView.imageURLs = model?.imageURLs
            
            //卸载约束对象
            self.bottomConstrain?.uninstall()
            
            if let url = retweetPicView.imageURLs where url.count != 0{
            // 转发微博中有图片的时候
                retweetPicView.hidden = false
                
                retweetPicView.imageURLs = url
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                  self.bottomConstrain =  make.bottom.equalTo(retweetPicView.snp_bottom).constraint
                })
            }else{
            //转发微博中没有图片的时候
                
                retweetPicView.hidden = true

                //设置imageURLs=nil  防止重用
                retweetPicView.imageURLs = nil
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                  self.bottomConstrain =  make.bottom.equalTo(retweetLabel.snp_bottom).constraint
                })
                
            }
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
    
        addSubview(retweetLabel)
        addSubview(retweetPicView)
        
        //设置约束
        retweetLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
        }
        
        retweetPicView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(retweetLabel.snp_bottom).offset(statusMagin)
            
            make.left.equalTo(self.snp_left)
        }
        
        self.snp_makeConstraints { (make) -> Void in
            
           self.bottomConstrain = make.bottom.equalTo(retweetPicView.snp_bottom).constraint
        }
    }
    
    //懒加载
    //转发的文字
   private lazy var retweetLabel:UILabel = UILabel.labelWith("转发狂魔", textColor: UIColor.darkGrayColor(), textAlignment: .Left, textFont: 14)
    
    //转发的图片
   private lazy var retweetPicView : XCPictureView = XCPictureView()
}
